﻿using Mate.Caching;
using Mate.Core;
using Mate.Db.Model.System;
using Mate.Db.Model.Users;
using Mate.Db.SqlSugar;
using Mate.Dto.Users;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace Mate.User.Service
{
    public class AdminUserService: DataService<admin_user> ,IAdminUserService
    {

        private readonly ICacheManager _cache;
        public AdminUserService(DataClient dataClient, ICacheManager cache) : base(dataClient)
        {
            this._cache = cache;
        }

        /// <summary>
        /// 获取当前登录用户
        /// </summary>
        /// <returns></returns>
        public admin_user CurrentAdminUser(int userId)
        {
            if (userId == 0)
            {
                return null;
            }
            return _cache.Get(string.Format(MateCacheKey.CacheUserIdKey, userId.ToString()), () =>
            {
                return Get(x => x.id == userId);
            });
        }

        #region 根据用户名密码获取
        /// <summary>
        /// 根据用户名密码获取
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public admin_user GetByUserNameAndPwd(string userName, string password)
        {
            return dataClient.Queryable<admin_user>().First(x => x.user_no == userName
                                                    && x.password == password && x.is_delete == false);
        }
        #endregion

        #region 更新最后登录时间
        /// <summary>
        /// 更新最后登录时间
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <returns></returns>
        public bool UpdateLastLoginTime(int id)
        {
            return dataClient.Updateable(new admin_user()
            {
                id = id,
                last_logintime = DateTime.Now
            }).UpdateColumns(x => new object[] { x.last_logintime }).ExecuteCommand() > 0;
        }
        #endregion

        #region 获取列表
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="Key">用户名,电话</param>
        /// <param name="status">状态</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="recordCount">总条数</param>
        /// <returns></returns>
        public IList<AdminUserModel> GetList(int pageIndex, int pageSize, string Key, ref int recordCount)
        {
            var queryable=dataClient.Queryable<admin_user,roles>((a,r)=>new object[]{
                JoinType.Left,a.roles_id == r.id,
                }).Where(a => a.is_delete == false);
            if (!string.IsNullOrWhiteSpace(Key))
            {
                queryable.Where(a => a.user_name == Key || a.user_no == Key);
            }
      
            return queryable.Select<AdminUserModel>("a.*,r.role_name")
                .OrderBy(a => a.id, OrderByType.Desc)
                .ToPageList(pageIndex, pageSize, ref recordCount);
        }
        #endregion

    }
}
